package greedy.onClass.class1.exer01;

import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        PriorityQueue<Integer> pq = new PriorityQueue<>((e1, e2) -> e1 - e2);
        for (int i = 0; i < n; i++) {
            pq.offer(sc.nextInt());
        }
        int costSum = 0;
        while (!pq.isEmpty() && pq.size() > 1) {
            int minStone = pq.poll();
            int secMinStone = pq.poll();
            int sum = minStone + secMinStone;
            costSum += sum;
            pq.offer(sum);
        }
        System.out.println(costSum);
        sc.close();
    }
}
